package panel.selfPathList
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	
	public class SelfPathEditBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var backImg:Bitmap;
		
		private var icon:ImageResource;
		
		private var upBtn:ButtonUtils;
		private var downBtn:ButtonUtils;
		private var deleteBtn:ButtonUtils;
		
		private var dateBox:DateChangeBox;
		
		private var titleText:TextField;
		
		private var moveUpThis:Function;
		private var moveDownThis:Function;
		private var deleteThisObj:Function;
		
		private var reListThis:Function;
		
		public var info:*;
		
		public function SelfPathEditBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			backImg = new _loader.uiManager.componentBack as Bitmap;
			backImg.width = _stage.stageWidth;
			backImg.height = _stage.stageHeight * .2;
			this.addChild(backImg);
			
			titleText = new TextField;
			titleText.wordWrap = false;
			titleText.multiline = false;
			titleText.selectable = false;
			
			this.addChild(titleText);
			titleText.x = _stage.stageWidth * .02;
			titleText.y = _stage.stageHeight * .16;
			
			var img1:Bitmap = new _loader.uiManager.arrowUp as Bitmap;
			img1.width = _stage.stageHeight * .06;
			img1.height = _stage.stageHeight * .06;
			img1.smoothing = true;
			
			var img2:Bitmap = new _loader.uiManager.arrowDown as Bitmap;
			img2.width = _stage.stageHeight * .06;
			img2.height = _stage.stageHeight * .06;
			img2.smoothing = true;
			
			var img3:Bitmap = new _loader.uiManager.deleteBtn as Bitmap;
			img3.width = _stage.stageHeight * .06;
			img3.height = _stage.stageHeight * .06;
			img3.smoothing = true;
			
			upBtn = new ButtonUtils(img1,upThis);
			downBtn = new ButtonUtils(img2,downThis);
			deleteBtn = new ButtonUtils(img3,deleteThis);
			
			this.addChild(upBtn);
			upBtn.y = _stage.stageHeight * .2 / 2 - upBtn.height - 5;
			upBtn.x = _stage.stageWidth * 2.7 / 4;
			
			this.addChild(downBtn);
			downBtn.y = _stage.stageHeight * .2 / 2 + 5;
			downBtn.x = _stage.stageWidth * 2.7 / 4;
			
			this.addChild(deleteBtn);
			deleteBtn.y = (_stage.stageHeight * .2 - deleteBtn.height) / 2;
			deleteBtn.x = _stage.stageWidth * 6 / 7;
			
			dateBox = new DateChangeBox(_stage,_loader,_lang,_uri);
			dateBox.initThis(_stage.stageWidth * .25,_stage.stageHeight * .1);
			this.addChild(dateBox);
			
			dateBox.x = _stage.stageWidth * .35;
			dateBox.y = (_stage.stageHeight * .2 - dateBox.height) / 2;
		}
		
		public function changeData(obj:*):void
		{
			info = obj;
			
			dateBox.changeCallBack = reListThis;			
			
			if(icon != null){
				icon = null;
			}
			
			icon = new ImageResource(_loader,obj.url,reposition);
			this.addChild(icon);
			
			icon.x = _stage.stageWidth * .03;
			icon.y = 0;
			
			function reposition():void
			{
				icon.width = _stage.stageHeight * .15;
				icon.height = _stage.stageHeight * .15;
			}
			
			TextFieldUtils.setHtmlText(titleText,HtmlText.formatSizeColor(obj.name,0x000000,18));
			
			titleText.width = titleText.textWidth + 5;
			titleText.height = titleText.textHeight + 5;
			
			//============================
		}
		
		private function upThis():void
		{
			if(moveUpThis is Function){
				moveUpThis(info);
			}
		}
		
		private function downThis():void
		{
			if(moveDownThis is Function){
				moveDownThis(info);
			}
		}
		
		private function deleteThis():void
		{
			if(deleteThisObj is Function){
				deleteThisObj(info);
			}
		}
		
		public function set onUp(func:Function):void
		{
			moveUpThis = func;
		}
		
		public function set onDown(func:Function):void
		{
			moveDownThis = func;
		}
		
		public function set onDelete(func:Function):void
		{
			deleteThisObj = func;
		}
		
		public function set onReList(func:Function):void
		{
			reListThis = func;
		}
		
		public function clear():void
		{
			if(_stage){
				_stage = null;
				_loader = null;
				_lang = null;
				_uri = null;
				
			}
		}
		
		public function set date(num:int):void
		{
			if(dateBox){
				dateBox.changeCount(num + 1);
			}
		}
		
		public function get currentDate():int
		{
			if(dateBox){
				return dateBox.currentDate;
			}
			return 1;
		}
		
		public override function get height():Number
		{
			return _stage.stageHeight * .2;
		}
	}
}